package com.syh.jn.scm.domain.operation.entity.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.jn.scm.domain.operation.repository.system.OperationAccountRepository;
import com.syh.jn.scm.domain.operation.repository.system.OperationMenuRepository;
import com.syh.jn.scm.domain.operation.repository.system.OperationRoleRepository;
import com.syh.jn.scm.domain.support.SuperBusinessAggregate;
import com.syh.jn.scm.dto.operation.system.permission.PermissionVo;
import com.syh.jn.scm.dto.operation.system.role.OperationRoleMto;
import com.syh.jn.scm.dto.operation.system.role.OperationRoleSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class OperationRole extends SuperBusinessAggregate<Long> {


  public OperationRole(OperationRoleSto sto) {
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);
		this.terminalType = "OPERATION";
    this.createTimeStamp = System.currentTimeMillis();
    ExtBeanUtils.copyProperties(sto, this, false);
  }

  public void modify(OperationRoleMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
    ExtBeanUtils.copyProperties(mto, this, false);
  }

  public void updateMenus(List<Long> menuIds) {
    if (menuIds.isEmpty()) {
      this.operationMenuList = Collections.emptyList();
      return;
    }
    OperationMenuRepository operationMenuRepository = (OperationMenuRepository) SpringContextUtils.getBean(OperationMenuRepository.class);
    List<OperationMenu> operationMenuList = operationMenuRepository.getList(Wrapper.build().append(Restrictions.in("id", menuIds.toArray())));
    this.operationMenuList = operationMenuList;
  }

  /**
   * 数据删除
   */
  public void destroy() {
		OperationRoleRepository operationRoleRepository = (OperationRoleRepository) SpringContextUtils.getBean(OperationRoleRepository.class);
		List<PermissionVo> permission = operationRoleRepository.getPermissionByRoleId(id);
		if (permission != null && permission.size() > 0) {
			throw new BusinessException("该角色存在菜单,不能删除");
		}
		OperationAccountRepository accountRepository = (OperationAccountRepository) SpringContextUtils.getBean(OperationAccountRepository.class);
		Wrapper wrapper = Wrapper.build();
		wrapper.append(
						Restrictions.and(Restrictions.eq("roleId", id))
		);
		List accounts = accountRepository.findListPage(wrapper, 0, 1000);
		if (accounts != null && accounts.size() > 0) {
			throw new BusinessException("角色不能删除,已有账号正在使用");
		}
  }

  /**
   * 角色名称
   */
  private String roleName;
	private String terminalType;
	private String description;
	private Integer orderLevel;
  private List<OperationMenu> operationMenuList;
	private Long businessEntityId;
}
